#include "form.h"
#include "Report/login.h"
#include "qcalendarwidget.h"
#include "ui_form.h"
#include "QFile"
#include "QDebug"
#include "QTextCharFormat"
#include "QSpinBox"
#include "QToolButton"
#include "HRVPro/hrvdataprocess.h"
#include "qrandom.h"
#include "QPrinter"
#include "QDir"
Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    appgalg=new AppgAlg();
//    QColor style_main_color(00,255,30);
    ui->dateEdit->setCalendarPopup(true); //设置日历弹出
    ui->dateEdit->setStyleSheet(QString("QDateEdit{border-radius: 6px;border: 1px solid rgba(0,200,100,255);padding:3px 0px 3px 6px;color: #000000;selection-background-color:rgba(0,200,100,255);}"
 "QDateEdit::drop-down{border:none;}"
 "QDateEdit::down-arrow{image: url(:xiala_black.png);border: none;}"));

    QTextCharFormat format;//字体风格：黑色
    format.setForeground(QColor(Qt::black));
    QCalendarWidget * dateEditCalendarWidget = ui->dateEdit->calendarWidget();
    dateEditCalendarWidget->setWeekdayTextFormat(Qt::Saturday, format);
    dateEditCalendarWidget->setWeekdayTextFormat(Qt::Sunday, format);
    dateEditCalendarWidget->setFixedSize(500,300);
    dateEditCalendarWidget->findChildren<QWidget*>().value(0)->setCursor(Qt::PointingHandCursor);
    dateEditCalendarWidget->findChildren<QSpinBox*>().value(0)->setAlignment(Qt::AlignCenter);
    dateEditCalendarWidget->findChildren<QSpinBox*>().value(0)->setButtonSymbols(QSpinBox::NoButtons);
    QList<QToolButton * > toolbtn_list = ui->dateEdit->calendarWidget()->findChildren<QToolButton*>();
    for (int var = 0; var < toolbtn_list.size(); ++var)
    {
 toolbtn_list.value(var)->setCursor(Qt::PointingHandCursor);
    }
    QSize s = QSize(32,32);
    QToolButton * toolbtn  = ui->dateEdit->calendarWidget()->findChild<QToolButton * >("qt_calendar_prevmonth");
    toolbtn->setIcon(QPixmap(":left_ar.png"));
    toolbtn->setIconSize(s);
    toolbtn = ui->dateEdit->calendarWidget()->findChild<QToolButton * >("qt_calendar_nextmonth");
    toolbtn->setIcon(QPixmap(":right_ar.png"));
    toolbtn->setIconSize(s);

//
    ui->dateEdit->calendarWidget()->setStyleSheet(QString("QCalendarWidget{background-color:#FFFFFF;border: 1px solid rgba(0,200,100,255);}"
     "QCalendarWidget QAbstractItemView:enabled{color:#000000;"
     "background-color:#ffffff;"
     "selection-color: white;"
     "selection-background-color:rgba(0,200,100,255);}"
     "QCalendarWidget QSpinBox#qt_calendar_yearedit{background:#ffffff;height:34px;width:125px;selection-background-color:rgba(0,200,100,255);}"
     "QCalendarWidget QToolButton{background-color:#FFFFFF;height:34px;width:125px;color:#000000;}"
     "QCalendarWidget QToolButton:hover{border: 1px solid rgba(0,200,100,255);}"
     "QCalendarWidget QToolButton::menu-indicator#qt_calendar_monthbutton{subcontrol-position: right center;subcontrol-origin: padding;}"
     "QCalendarWidget QToolButton QMenu{background-color:#FFFFFF;width:125px;border:1px solid rgba(0,200,100,255);}"
     "QCalendarWidget QToolButton QMenu::item:selected{color:#FFFFFF;background:rgba(0,200,100,255);}"));
    ui->dateEdit->setDate(QDate::currentDate());
    ui->dateEdit->setDisplayFormat("yyyy.MM.dd");

}

Form::~Form()
{
    delete appgalg;
    delete ui;
}

void Form::on_pushButton_clicked()
{
    QFile file("C:\\Users\\haibi\\Desktop\\ppg.txt");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
 qDebug()<<"open==";
 return;
    }else{
 QTextStream in(&file);
 QString s=in.readAll();
 file.close();
 QStringList sl=s.split(",");
 int len=sl.size();
 QList<double>ppg;
 for(int i=0;i<len;i++)
     ppg.append(sl.at(i).toDouble());

// for(int i=0;i<len-3050;i=i+500){
//     QList<double>sub_ppg=ppg.mid(i,3000);
//     appgalg->computePrensentPPG_APPG(sub_ppg);
//     appgalg->computeAccIndex(40);

//     qDebug()<<appgalg->abcde;
// }
 QList<double>sub_ppg=ppg.mid(0,3000);
 appgalg->computePrensentPPG_APPG(sub_ppg);
 appgalg->computeAccIndex(40);
    }

}


void Form::on_pushButton_2_clicked()
{
    HRVDataProcess hrvDataProcess;
   // int *rr=new int[30];
    // float rr1[]={930,944,982,1000,972,904,908,922,918,934,962,980,948,958,970,982,968,998,1022,998,954,938,934,906,952,976,976,942,952,964,934,956,984,970,962,990,984,1012,1042,1020,1026,1028,956,946,958,966,938,942,988,970,998,1010,1012,1020,986,960,980,958,966,970,980,1022,1016,970,968,962,912,876,898,926,908,868,888,926,910,922,930,948,926,974,1048,1032,1024,1030,996,1008,1034,1054,978,944,936,940,932,984,982,980,1014,1014,988,1036,1070,1046,1062,1042,1004,1008,1036,1016,1038,1070,1054,1068,1112,1038,1054,1092,1056,1044,1070,1048,1042,1076,1096,1018,1046,1050,1028,1022,1112,1078,1022,1060,1094,1086,1124,1142,1122,1068,1088,1110,1076,1042,1078,1100,1052,1072,1096,1052,1070,1072,1010,1050,1052,1004,1018,1040,1010,1058,1086,1066,1080,1092,1064,1088,1066,1054,1068,1018,984,966,904};
    // int *rr=new int[171];
    // for(int i=0;i<171;i++)
    // {
    //     rr[i]=rr1[i];
    // }
    QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "All Files (*);;Text Files (*.txt)");
    QFile aFile(fileName);
    if (!aFile.exists()) //文件不存在
        return;

    QTextStream stram(&aFile);     //纯文本文件流
    QList<int>rr;
    int bufLen=90000;
    int *p_data_buf=new int[bufLen];
    for(int j=0;j<bufLen;j++)
        p_data_buf[j]=0;
    int j=0;
    if(aFile.open(QFile::ReadOnly | QFile::Text))
    {
        while (!stram.atEnd())
        {
            QString line = stram.readLine();
            int v=line.toInt();
            p_data_buf[j++]=v;
            rr.append(v);
            // qDebug()<<v;
        }
        aFile.close();
    }
    hrvDataProcess.setAge(42,0);
    hrvDataProcess.ComputeHRV(p_data_buf,bufLen);
    HRVIndex hrvIndex=hrvDataProcess.getHRVIndex();
    qDebug()<<"RR_mean="<<hrvIndex.time.RR_mean;
    qDebug()<<"RR_CV="<<hrvIndex.time.RR_CV;
    qDebug()<<"RR_SDNN="<<hrvIndex.time.RR_SDNN;
    qDebug()<<"RR_DRR="<<hrvIndex.time.RR_DRR;
    qDebug()<<"RR_RMSSD="<<hrvIndex.time.RR_RMSSD;
    qDebug()<<"SDSD="<<hrvIndex.time.SDSD;
    qDebug()<<"NN50="<<hrvIndex.time.NN50;
    qDebug()<<"pNN50="<<hrvIndex.time.pNN50;

    qDebug()<<"TP="<<hrvIndex.freq.TP;
    qDebug()<<"VLF="<<hrvIndex.freq.VLF;
    qDebug()<<"LF="<<hrvIndex.freq.LF;
    qDebug()<<"HF="<<hrvIndex.freq.HF;
    qDebug()<<"LFNorm="<<hrvIndex.freq.LFnorm;
    qDebug()<<"HFnorm="<<hrvIndex.freq.HFnorm;
    qDebug()<<"LFHF="<<hrvIndex.freq.LFHF;

    qDebug()<<"EmotionScore="<<hrvIndex.mp.EmotionScore;
    qDebug()<<"tpScore="<<hrvIndex.mp.tpScore;
    qDebug()<<"lfScore="<<hrvIndex.mp.lfScore;
    qDebug()<<"hfScore="<<hrvIndex.mp.hfScore;

}


void Form::on_pushButton_3_clicked()
{
    QPrinter printer,printer2;
    printer.setPageSize(QPageSize::A4);
    printer2.setPageSize(QPageSize::A4);
    //输出文件到d盘data文件夹

    QString ParentFolder="D:\\data\\gg";
    QDir dir(ParentFolder);
    QString fileName;
    if(dir.exists())
    {
        fileName=ParentFolder+"\\"+"1.png";
    }else
    {
    bool ok = dir.mkpath(ParentFolder);//创建多级目录
    if(ok)
    {
     fileName=ParentFolder+"\\"+"1.png";
    }
    }
    printer.setOutputFileName(fileName);

    printer.setResolution(300);//值越大分辨率越高
    printer.setPageMargins(QMarginsF(0, 0, 0, 0));
    Login report1;


    QPixmap pixmap1=report1.grab(report1.rect()); //抓取界面widget区域，可以抓取任意控件区域

    pixmap1.save(fileName);
//    QPainter painter;
//    painter.begin(&printer);
//    painter.drawPixmap(0,0,pixmap1); //按照坐标画图
//    painter.end();
}

